$(document).ready(function () {
	//时间格式化函数
	Date.prototype.Format = function (fmt) { //author: meizz
		var o = {
			"M+": this.getMonth() + 1, //月份
			"d+": this.getDate(), //日
			"h+": this.getHours(), //小时
			"m+": this.getMinutes(), //分
			"s+": this.getSeconds(), //秒
			"q+": Math.floor((this.getMonth() + 3) / 3), //季度
			"S": this.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
		//调用  new Date().Format("yyyy-MM-dd HH:mm:ss");
	};

	//数值、数值字符串  装换成货币格式函数
	Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
		places = !isNaN(places = Math.abs(places)) ? places : 2;
		symbol = symbol !== undefined ? symbol : "";
		thousand = thousand || ",";
		decimal = decimal || ".";
		var number = this,
			negative = number < 0 ? "-" : "",
			i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
			j = (j = i.length) > 3 ? j % 3 : 0;
		return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
	};

	//切换类型  普通、黄金 、白金、钻石
	switch_status();
	function switch_status() {
		var recharge_status_general = $('.recharge_status_general');//普通
		var recharge_status_gold = $('.recharge_status_gold');//黄金
		var recharge_status_platina = $('.recharge_status_platina');//白金
		var recharge_status_jewel = $('.recharge_status_jewel');//钻石
		var recharge_status_mouth = $('.recharge_status_mouth');//口腔
		$('.recharge_status').delegate('.text-center','click',function () {
			var span = $(this).find('span');
			$('.recharge_status').find('span').removeClass('active');//删除所有活动类
			span.addClass('active');//当前点击项添加活动类

			if(span.text() == '普通'){
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_general.addClass('active');//展示普通
			}

			if(span.text() == '黄金'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_gold.addClass('active');//展示黄金
			}

			if(span.text() == '白金'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_platina.addClass('active');//展示隐藏白金
			}

			if(span.text() == '钻石'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_jewel.addClass('active');//展示钻石
			}

			if(span.text() == '口腔'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.addClass('active');//展示口腔
			}

		});
	}

	//选择普通账户类型  普通/体验
	select_jollyone_project();
	function select_jollyone_project() {
		var select = $('#select_project');
		var general = $('#recharge_status_general');
		var account_detail = general.find('.account_detail');
		var account_detail_experience = general.find('.account_detail_experience');
		var btn = general.find('.account_money_recharge');//充值按钮

		select.delegate('.pull-left','click',function () {
			select.find('.active').removeClass('active');
			$(this).addClass('active');
			$('#recharge_status_general').find('.account_money_format').text( $(this).attr('rest_account') );

			if( $(this).text() == '普通' ){
				account_detail_experience.removeClass('active');
				account_detail.addClass('active');
				btn.css('display','inline-block');
			}

			if( $(this).text() == '体验' ){
				account_detail.removeClass('active');
				account_detail_experience.addClass('active');
				btn.css('display','none');
			}
		});
	}

	//设置滑动列表高度  超出滑动
	set_height_overflow('recharge_status_general');
	set_height_overflow('recharge_status_platina');
	set_height_overflow('recharge_status_jewel');
	set_height_overflow('recharge_status_mouth');
	function set_height_overflow(contentId) {
		var con = $('#' + contentId).find('.account_detail_list');//超出滑动容器
		var recharge_status = $('.recharge_status');
		var nav = $('.nav');
		var account_money = $('#recharge_status_general').find('.account_money');
		var account_money_next = $('#recharge_status_general').find('.account_money').next();
		var account_detail_list_prev = $('#recharge_status_general').find('.account_detail_list').prev();
		var select_project = $('#select_project');
		var sum = 0;
		if( contentId == 'recharge_status_general' ){
			sum = recharge_status.height() + nav.outerHeight() + account_money.outerHeight() + account_money_next.height() + account_detail_list_prev.outerHeight() + select_project.height();
		}else {
			sum = recharge_status.height() + nav.outerHeight() + account_money.outerHeight() + account_money_next.height() + account_detail_list_prev.outerHeight();
		}


		con.css('maxHeight', $(window).height() - sum );
		con.css('overflow', 'auto' );
	}

	//获取账户余额  及  账户明细  普通-普通、白金、钻石
	get_account_money_and_detial('recharge_status_general','1');//普通
	get_account_money_and_detial('recharge_status_gold','5');//黄金
	get_account_money_and_detial('recharge_status_platina','2');//白金
	get_account_money_and_detial('recharge_status_jewel','3');//钻石
	get_account_money_and_detial('recharge_status_mouth','6');//口腔
	function get_account_money_and_detial(content,type) {
		$.ajax({
			url: '/user/center',
			type:'POST',
			dataType: 'json',
			data: {
				type: type
			},
			success:function (data) {
				if( data.code == '1' ){
					if( data.data.score_detail != '' && data.data.score_detail != null ){
						fill_content_in_page(content,data.data.score_detail);//填充账户余额  及  生成账户明细插入页面  普通-普通、白金、钻石、口腔
					}else {
						var html = '';
						html += '<div style="background-color: #f2f2f2;padding-top: 3rem;">';
						html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
						html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无账户明细</div>';
						html += '</div>';
						$('#' + content).find('.account_detail').append( html )
					}

					if( content == 'recharge_status_general' ){
						$('#select_project').find('.pull-left').eq(0).attr( 'rest_account' , parseFloat( data.data.user_score ).formatMoney() );
					}
					$('#' + content).find('.account_money_format').text( parseFloat( data.data.user_score ).formatMoney() );
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//填充账户余额  及  生成账户明细插入页面  普通-普通、白金、钻石、口腔
	function fill_content_in_page(content,data) {
		/*
		* detail_type:  1:充值  2:消费
		* */
		var div = $('#' + content);
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var status = '';
			var status_text = '';

			// is_exchange  0: 否(非兑换)  1：是（兑换）
			if( data[i].is_exchange  == '0' ){
				if( data[i].detail_type == '1' ){
					status_text = '充值';
					status = '+';
				}
				if( data[i].detail_type == '2' ){
					status_text = '购买项目';
					status = '-';
				}
				if( data[i].detail_type == '3' ){
					status_text = '取消订单';
					status = '+';
				}
			}else {
				status_text = '兑换';
				if( data[i].score_type == '1' ){
					status = '-';
				}else {
					status = '+';
				}
			}

			var html = '';
			html += '<div class="row">';
			html += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">'+ status_text +'</div>';
			html += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 text-right">';
			html += '<div>'+ new Date( data[i].add_at * 1000 ).Format("yyyy-MM-dd") +'</div>';
			html += '<div>'+ status+ parseFloat( data[i].detail_score ).toFixed(2) +'</div>';
			html += '</div>';
			html += '</div>';

			div.find('.account_detail_list').append( html )
		}
	}

	//获取账户余额  及  账户明细  普通-体验
	get_account_money_and_detial_experience('recharge_status_general','4');//普通-体验
	function get_account_money_and_detial_experience(content,type) {
		$.ajax({
			url: '/user/center',
			type:'POST',
			dataType: 'json',
			data: {
				type: type
			},
			success:function (data) {
				if( data.code == '1' ){
					if( data.data.score_detail != '' && data.data.score_detail != null ){
						fill_content_in_page_experience(content,data.data.score_detail);
					}else {
						var html = '';
						html += '<div style="background-color: #f2f2f2;padding-top: 3rem;">';
						html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
						html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无账户明细</div>';
						html += '</div>';
						$('#' + content).find('.account_detail_experience').append( html )
					}

					$('#select_project').find('.pull-left').eq(1).attr( 'rest_account' , parseFloat( data.data.user_score ).formatMoney() );
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//填充账户余额  及  生成账户明细插入页面  普通-体验
	function fill_content_in_page_experience(content,data) {
		/*
		* detail_type:  1:充值  2:消费
		* */
		var div = $('#' + content);
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var status = '';
			var status_text = '';

			// is_exchange  0: 否(非兑换)  1：是（兑换）
			if( data[i].is_exchange  == '0' ){
				if( data[i].detail_type == '1' ){
					status_text = '充值';
					status = '+';
				}
				if( data[i].detail_type == '2' ){
					status_text = '购买项目';
					status = '-';
				}
				if( data[i].detail_type == '3' ){
					status_text = '取消订单';
					status = '+';
				}
			}else {
				status_text = '兑换';
				if( data[i].score_type == '1' ){
					status = '-';
				}else {
					status = '+';
				}
			}

			var html = '';
			html += '<div class="row">';
			html += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">'+ status_text +'</div>';
			html += '<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 text-right">';
			html += '<div>'+ new Date( data[i].add_at * 1000 ).Format("yyyy-MM-dd") +'</div>';
			html += '<div>'+ status+ parseFloat( data[i].detail_score ).toFixed(2) +'</div>';
			html += '</div>';
			html += '</div>';

			div.find('.account_detail_experience_list').append( html )
		}
	}

	//点击充值按钮对账户进行充值
	click_account_money_recharge('recharge_status_general');//普通
	click_account_money_recharge('recharge_status_gold');//黄金
	click_account_money_recharge('recharge_status_platina');//白金
	click_account_money_recharge('recharge_status_jewel');//钻石
	click_account_money_recharge('recharge_status_mouth');//口腔
	function click_account_money_recharge(content) {
		$('#' + content).find('.account_money_recharge').on('click',function () {
			window.location = '/index/user_center_recharge'
		});
	}

	/************************************返回上一页***************************/
	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}
});